package com.example.ceshi.controller;

import com.example.ceshi.entity.User;
import com.example.ceshi.entity.UserSpread;
import com.example.ceshi.mapper.UserMapper;
import com.example.ceshi.mapper.UserSpreadMapper;
import com.example.ceshi.service.UserSpreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @controller 声明控制器类
 * @service 声明service层
 * @repository 声明持久化类 dao层
 * @component 除了控制器service 和dao层之外的类，一律使用次注解声明
 * @postconstruct 声明一个方法为当前类的构造方法
 * @prodestroy 声明一个销毁方法
 */
@Slf4j
@RestController
@Api(tags = "测试-HelloWorld") //这个controller下的所有接口的描述
//@RequestMapping("/api")
public class Hello {
    @Autowired
    private UserMapper usermapper;
    @Autowired
    private UserSpreadMapper userSpreadMapper;

    private static final String UPLOADED_FOLDER = System.getProperty("user.dir")+"/upload";

    @ApiOperation("测试Swagger2") //显示在接口的信息说明
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    //car /2
    //PathVariable 路径变量，获取get路径中参数信息
    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getcar(@PathVariable Map<String, Object> huoqu) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", huoqu);
        return map;
    }

    @GetMapping("/user/{id}/owner/{username}")
    public Map<String, Object> getuser(@RequestParam Map<String, Object> getpa) {
        HashMap<String, Object> stringMap = new HashMap<>();
        stringMap.put("canshu", getpa);
        return stringMap;
    }

    @PostMapping("/save")
    public Map postmethon(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);


        return map;
    }

    @GetMapping("/selectall")
    public List<User> selectall() {
        List<User> users = usermapper.selectAllOrderByAgeDesc();
        System.out.println(users);
        return users;
    }

    /**
     * RequestParam 接受前段提交的参数
     *
     * @param aa
     * @requestheader 接受header请求头
     */
    @PostMapping("tijiao")
    public void tijiao(@RequestParam("email") String aa, @RequestHeader("Referer") String refer) {
        System.out.println(aa);
    }

    /*
     * requestbody 需要必须是ajax提交的数据才能用这个
     * */
    @RequestMapping("tijiao2")
    public void tijiao2(@RequestBody User user) {
        System.out.println(user);
    }

    /**
     * 表单直接提交的时候，使用user实体类接受值放到数据库中时用法
     *
     * @param user
     */
    @RequestMapping("tijiao3")
    public void tijiao3(User user) {
        System.out.println(user);
    }

    /**
     * 控制器同步响应 form 超链接，返回string实现页面 跳转
     * 通过redirect返回String类型跳转
     *  注意：这种方法不允许Spring控制器用@RestController注解，
     *  因为@RestController相当于类中的所有方法都标注了@ResponseBody，
     *  这些方法不会返回一个视图，而是返回一个json对象，这样的话只是在页面上打印出字符串，
     *  而不跳转。控制器用@Controller注解即可
     */
    @RequestMapping("tiaozhuan1")
    public String tiaozhuan1(String  email){
        System.out.println(email);
        return "2.html";
    }

    @RequestMapping("lists")
    //get请求用requestparam  post传递json参数用requestbody接收参数
    //List<String> likes 使用这个接收参数，他会默认认为是用的一个实体类来接收，但是却没有这个实体类所以报错，
    //所以需要在前头加上个RequestParam 来告诉他这个不是实体类接收，而是吧参数扔到list里头
    public String lists(@RequestParam List<String> likes){
        System.out.println("likes=>>"+likes);
        return "{'module':'list param'}";
    }

    @RequestMapping("lists2")
    //get请求用requestparam  post用requestbody接收参数
    //List<String> likes 使用这个接收参数，他会默认认为是用的一个实体类来接收，但是却没有这个实体类所以报错，
    //所以需要在前头加上个RequestParam 来告诉他这个不是实体类接收，而是吧参数扔到list里头
    //前段传值：["111","787878"]
    public String lists2(@RequestBody List<String> likes){
        System.out.println("likes=>>"+likes);
        return "{'module':'list param'}";
    }

    @RequestMapping("dates")
    public String datess(Date date, @DateTimeFormat(pattern = "yyyy-mm-dd") Date date1){
        System.out.println("shijianchuandi=>"+date);
        System.out.println("shijianchuandi2"+date1);
        return date1.toString();
    }

    @ApiOperation("caidanliebiao") //显示在接口的信息说明
    @RequestMapping("findnodes")
    public List<UserSpread> findnodes(){
       /* List<UserSpread> list = userSpreadMapper.findnodes();
        return list;*/
        List<UserSpread> userSpreads = userSpreadMapper.selectList(null);
        List<UserSpread> treeslist = UserSpreadService.trees(userSpreads);
        return  treeslist;
    }


    @ApiOperation("tianjia jiekou") //显示在接口的信息说明
    @RequestMapping("saveadd")
    public String  saveadd(@RequestBody UserSpread userSpread){
        userSpreadMapper.saveadd(userSpread);
        return "sucess";
    }

    @ApiOperation("提交表单")
    @PostMapping("tijiao66")
    public String tijiao66(@RequestParam("name") String name, @RequestPart("upload") MultipartFile upload) throws IOException{
        log.info("上传的信息：name={}.upload= {}",name,upload.getSize());
        if(!upload.isEmpty()){
            //save
            String originalFilename = upload.getOriginalFilename();
            upload.transferTo(new File("D:\\java\\"+originalFilename));
        }
        return "sss";
    }


}
